package com.dove.kuaizishareas.fragments;

import java.util.ArrayList;
import java.util.List;

import com.dove.kuaizishareas.R;
import com.dove.kuaizishareas.bean.Audio_file;
import com.dove.kuaizishareas.utils.ReadSDCardResource;
import com.dove.kuaizishareas.wificonnection.ChooseConnect;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class Audio extends Fragment {
	
	private ListView lv;
	private View view;
	private Context mContext;
	private List<Audio_file> list;
	private List<Audio_file> sendList;
	private Handler mHandler;
	private int i = 1;
	
	public Audio(Handler handler) {
		this.mHandler = handler;
	}
	
	public Audio(ChooseConnect chooseConnect) {
		this.mHandler = chooseConnect.getHandler();
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			 ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.audio, container, false);
		mContext = getActivity();
		
		initView();//初始化视图
		initData();//初始化数据
		
		return view;
	}

	private void initData() {
//		mHandler = new Choosecontactlocal().getHandler();
		sendList = new ArrayList<Audio_file>();
		list = new ArrayList<Audio_file>();
		new ReadSDCardResource().readSDCardMediaResourceByContentProvider(mContext,list);
		final MyAdapter adapter = new MyAdapter();
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Audio_file af = (Audio_file) adapter.getItem(position);
				sendList.add(af);
				Message mes = new Message();
				mes.obj = "send "+sendList.size();
				mHandler.sendMessage(mes);
			}
		});
	}

	private void initView() {
		lv = (ListView) view.findViewById(R.id.audio_listView);
	}
	
	private class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder vh = null;
			if(convertView==null){
				vh = new ViewHolder();
				convertView = LayoutInflater.from(mContext).inflate(R.layout.audio_list_item, null);
				vh.title = (TextView) convertView.findViewById(R.id.audio_list_item_title);
				vh.info = (TextView) convertView.findViewById(R.id.audio_list_item_info);
				convertView.setTag(vh);
			}else{
				vh = (ViewHolder) convertView.getTag();
			}
			Audio_file af = list.get(position);
			vh.title.setText(af.getTitle());
			vh.info.setText(af.getInfo());
			return convertView;
		}
		
		class ViewHolder{
			TextView title,info;
		}
	}

	
	public List<Audio_file> getSendList() {
		return sendList;
	}
	public void setSendList(List<Audio_file> sendList) {
		this.sendList = sendList;
	}
	
	
	
}
